/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;
/**
 *
 * @author darty
 */
  class CheckBoxNodeRenderer implements TreeCellRenderer {
  private JCheckBox leafRenderer = new JCheckBox();

  private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

  Color selectionBorderColor, selectionForeground, selectionBackground,
      textForeground, textBackground;

  protected JCheckBox getLeafRenderer() {
    return leafRenderer;
  }

  public CheckBoxNodeRenderer() {
    Font fontValue;
    fontValue = UIManager.getFont("Tree.font");
    if (fontValue != null) {
      leafRenderer.setFont(fontValue);
    }
    Boolean booleanValue = (Boolean) UIManager
        .get("Tree.drawsFocusBorderAroundIcon");
    leafRenderer.setFocusPainted((booleanValue != null)
        && (booleanValue.booleanValue()));

    selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
    selectionForeground = UIManager.getColor("Tree.selectionForeground");
    selectionBackground = UIManager.getColor("Tree.selectionBackground");
    textForeground = UIManager.getColor("Tree.textForeground");
    textBackground = UIManager.getColor("Tree.textBackground");
  }

  public JCheckBox getTreeCellRendererComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {

   // Component returnValue;
    leafRenderer.setEnabled(tree.isEnabled());

    if (leaf) {

      String stringValue = tree.convertValueToText(value, selected,
          expanded, leaf, row, true);
      //CheckBoxNode nn= new CheckBoxNode(stringValue,true);

 // leafRenderer.setText(stringValue);
      //leafRenderer.setText(nn.getText());
      //leafRenderer.setSelected(nn.isSelected());
// leafRenderer.setSelected(true);


    // leafRenderer.setEnabled(true);

    /*  if (selected) {
        leafRenderer.setForeground(selectionForeground);
        leafRenderer.setBackground(selectionBackground);
      } else {
        leafRenderer.setForeground(textForeground);
        leafRenderer.setBackground(textBackground);
      }
*/
      if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
        Object userObject = ((DefaultMutableTreeNode) value)
            .getUserObject();
        if (userObject instanceof CheckBoxNode) {
          CheckBoxNode node = (CheckBoxNode) userObject;
          // leafRenderer.setIcon(null);
           leafRenderer.setText(node.getText());
           leafRenderer.setSelected(node.isSelected());

        }
      }
     // returnValue = leafRenderer;
      return leafRenderer;
    } else {
       // checkbox.setIcon(null);
        //returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
         // value, selected, expanded, leaf, row, hasFocus);
        JCheckBox leafRenderer2=new JCheckBox();
/*
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
        Object userObject2 = ((DefaultMutableTreeNode) value)
            .getUserObject();
        if (userObject2 instanceof CheckBoxNode) {
          CheckBoxNode node2 = (CheckBoxNode) userObject2;
          // leafRenderer.setIcon(null);
           leafRenderer2.setText(node2.getText());
           leafRenderer2.setSelected(node2.isSelected());

        }
      }*/
        
          String stringValue = tree.convertValueToText(value, selected,
          expanded, leaf, row, false);

          CheckBoxNode nn= new CheckBoxNode(stringValue,selected);
           leafRenderer.setText(nn.getText());
           leafRenderer.setSelected(nn.isSelected());
           //leafRenderer.setEnabled(true);

           
          

       

        /*  CheckBoxNode node2 = (CheckBoxNode) (Object) value;
          // leafRenderer.setIcon(null);
           leafRenderer.setText(node2.getText());
           leafRenderer.setSelected(node2.isSelected());
*/
        


      

        
        /*Object userObject2 = ((DefaultMutableTreeNode) value)
            .getUserObject();
        if (userObject2 instanceof CheckBoxNode) {
          CheckBoxNode node2 = (CheckBoxNode) userObject2;
          // leafRenderer.setIcon(null);
           leafRenderer.setText(node2.getText());
           leafRenderer.setSelected(node2.isSelected());

        }
      */

 return leafRenderer;
    }
   // return returnValue;
   
  }
}